<?php
$this->breadcrumbs=array(
	'Issues'=>array('index'),
	'Assign',
);

?>

<h1>Editor's page</h1>
<h3>Danh sách bài tập cần được soạn thảo của editor</h3>

<div class="list-sort">
<?php $form=$this->beginWidget('CActiveForm', array(
	'action'=>Yii::app()->createUrl($this->route),
	'method'=>'get',
  'htmlOptions'=>array('class'=>'form-horizontal'),
)); ?>
<?php echo CHtml::dropDownList('sort', '', $model->getListSort(),array('class'=>'sort'))?>
	<i class="icon-chevron-down"></i> 
	<input type="hidden" value="desc" name='sort_type'>
<?php $this->endWidget(); ?>
</div>


<a id="btnReload"  class="btn btn-primary"><i class="icon-refresh icon-white"></i> Refresh trang</a>


<div class="grid-view">
<table border="1" class="items">
	<thead>
		<tr>
			<th>Issue</th>
			<th>Câu hỏi</th>						
			<th>Trạng thái bài giải</th>
			<th>Thời gian</th>
			<th>Action</th>		
		</tr>
	</thead>
	<tbody>

	
	<?php
		$this->widget('zii.widgets.CListView', array(
		'id'=>'edit-issue-list',
		'dataProvider'=>$model->assign($ID),
		'pager'=>array
		(
			'firstPageLabel' => '&lt;&lt;',
	    'prevPageLabel'  => '&lt;',
	    'nextPageLabel'  => '&gt;',
	    'lastPageLabel'  => '&gt;&gt;',
			'header'=>'',
		),
		'emptyText'=>'Không có dữ liệu.',
		'itemView'=>'_view_edit',
	)); 
	

?>
		</tbody>
</table>	
</div>


<style type="text/css">
	.defaultCountdown { width: 240px; height: 45px; }
</style>

<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->baseUrl; ?>/css/tables.css">
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->baseUrl; ?>/css/simplemodal/basic.css">
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl; ?>/js/countdown.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl; ?>/js/simplemodal/jquery.simplemodal.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl; ?>/js/simplemodal/basic.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl; ?>/js/simplemodal/popup.js"></script>
<script type="text/javascript">

		// Refresh page by click on Refresh button
		$(document).ready(function() {
		
				setupRefresh();
				
		    $('#btnReload').click(function() {
		            location.reload();
		      });
		}); 

		// Auto refresh after  10 minutes.
		window.onload = setupRefresh;
    function setupRefresh()
    {
        setInterval("refreshPage();",600000);
    }
    
    function refreshBlock()
    {
       $('#block1').load("index.html");
    }

    function refreshPage() {
        window.location = location.href;
    }    


		$(function() {
			$(".refresh").click(function() {
				console.log($(this));
				$(this).parent().children(".btn-success").removeAttr("disabled").removeClass("disabled");
				$(this).parent().children(".btn-inverse").removeAttr("disabled").removeClass("disabled");
			});

		$(".countdown").each(function() {
			if($(this).html()>0) {
				var arr_bt = $(this).attr("id").split("_");
			  var bt = arr_bt[1];
			  var issueID = arr_bt[2];
			  timer(
					  bt,
					  1000*($(this).html()),
					  function(timeleft) {
	
						  var time = "<button class='disabled btn btn-info'><i class='icon-time icon-white'></i>"+timeleft+"</button>"
							$("#countdown_"+bt+"_"+issueID).html(time);
							
						},
						function() {
							submitEdit(issueID, bt);
						}
				);
			}
		});
});

function submitEdit(issueID, answerID) {
	var resolverID = <?php echo Yii::app()->user->id?>;

	$.ajax({
		url: baseUrl + "/admin/issue/setEditSubmit",
		data: {issueID: issueID, resolverID: resolverID, answerID: answerID},
		type: "POST",//or GET
		success: function(data) {
			if(data=="success") {		
				$("#countdown_"+answerID+"_"+issueID).html("<button class='disabled btn btn-warning'><i class='icon-time icon-white'></i> Hết giờ</button>");
			}			
		}
	});
}


function beforeEdit(issueID,num,status) {
	var resolverID = $("#issue_"+issueID+"_"+num).val();
	$.ajax({
		url: baseUrl + "/admin/issue/setEdit",
		data: {issueID: issueID, resolverID: resolverID, status: status, num: num},
		type: "POST",//or GET
		success: function(data) {
			if(data=="success") {
				alert(data);
				$("#"+status+"_"+issueID+"_"+num).addClass("disabled").attr("disabled","disabled");
			}
		}
	});

}
</script>